<?php
include ("config.php");
include ("class.php");

### Далее идут функции. Желательно их не править

function Openconn()
	{
		global $db_host, $db_port, $db_user, $db_pass;
		mysql_connect("$db_host:$db_port", "$db_user", "$db_pass") or die (mysql_error());
	}
	
function SelectDB($db)
	{
		global $auth, $char, $imwcp, $db_encode;
		mysql_select_db("${$db}")or die("Could not select database");
		mysql_query("SET CHARACTER SET $db_encode");
		mysql_query("SET NAMES $db_encode");
	}
	
function  pprintcharlist()
	{
		$uid = $_SESSION["uid"];
		SelectDB(char);
		$select = mysql_query("SELECT * FROM `characters` WHERE `account`='$uid'");
		echo "<table>";
		echo "<tr><td>Имя</td><td>Класс</td><td>Раса</td><td>Уровень</td></tr>";
		while($chars = mysql_fetch_array($select)) {
			echo "<tr><td><a href=?do=main&chr=".$chars['guid'].">".$chars['name']."</a></td><td><img src=./template/default/img/class/".$chars['class'].".png></td><td><img src=./template/default/img/race/".$chars['race']."-".$chars['gender'].".png></td><td>".$chars['level']."</td></tr>";
		}
		echo "</table>"; 
		mysql_free_result($select);
	}
	
function charinfo($chr)
	{
		settype($chr, "int");
		if($chr<1) {
			return false;
		} else {
			SelectDB(char);
			$q  = "SELECT * FROM `characters` WHERE `guid`='%d'";
			$query = sprintf($q, $chr);
			$select = mysql_query($query);
			$chars = mysql_fetch_array($select);
			
			$money = $chars['money'];
			$money_g = (int)($money/10000);
			$total_money = $money - ($money_g*10000);
			$money_s = (int)($total_money/100);
			$money_c = $total_money - ($money_s*100);
			
			echo "<center><table>";
			echo "<tr><td>Имя</td><td>Класс</td><td>Раса</td><td>Уровень</td><td>Очков чести</td><td>Деньги</td><td>Исправить</td></tr>";
			echo "<tr><td>".$chars['name']."</td><td><img src=./template/default/img/class/".$chars['class'].".png></td><td><img src=./template/default/img/race/".$chars['race']."-".$chars['gender'].".png></td><td>".$chars['level']."</td><td>".$chars['totalHonorPoints']."</td><td>".$money_g."  з".$money_s." с".$money_c." м</td><td><input type='button' onclick=location.href='?do=main&chr=$chr&unbug=1' value='Исправить'></td></tr>";
			echo "</table></center>";
		}
	}
	
function charinacc($chr, $uid)
	{
		SelectDB(char);
		$q  = "SELECT * FROM `characters` WHERE `guid`='%d' AND `account`='%d'";
		$query = sprintf($q, $chr, $uid);
		$select = mysql_query($query);
		$count = mysql_num_rows($select);
		if($count==1) { return true; } else { return false; }		
	}

function chrfix($chr)
	{
		$uid = $_SESSION["uid"];
		SelectDB(char);
		$q  = "SELECT * FROM `character_homebind` WHERE `guid`='%d'";
		$query = sprintf($q, $chr);
		$select = mysql_query($query);
		$chars = mysql_fetch_array($select);
		$px = $chars['position_x']; $py = $chars['position_y']; $pz = $chars['position_z']; $pm = $chars['map'];
		if(charinacc($chr, $uid)) {
		mysql_query("UPDATE `characters` SET `position_x`='$px', `position_y`='$py', `position_z`='$pz', `map`='$pm' WHERE `guid`='$chr'");
		mysql_query("DELETE * FROM `character_aura` WHERE `guid`='$chr'");
		mysql_query("DELETE * FROM `group_member` WHERE `memberGuid`='$chr'");
		return "<center><b>Персонаж исправлен!</b></center>";
		} else {
			return "<center><b>Ошибка!</b></center>";
		}
	}
	
function getbonuses()
	{
		SelectDB(auth);
		$uid = $_SESSION["uid"];
		$select = mysql_query("SELECT `bonuses` FROM `account` WHERE `id`='$uid'");
		$array = mysql_fetch_array($select);
		$bonuses = $array['bonuses'];
		return $bonuses;
	}
	
function GetDataTime($timestamp = 0) 
	{
	if ($timestamp)
		return date("Y-m-d H:i:s", $timestamp);
	else
		return date("Y-m-d H:i:s");
	}
	
function gmtime() 
	{
	return strtotime(GetDataTime());
	}
	
function GetGMLevel($uid)
	{
		$select = mysql_query("SELECT `gmlevel` FROM `account_access` WHERE `id`='$uid' AND `RealmID`='1'");
		$count = mysql_num_rows($select);
		$array = mysql_fetch_array($select);
		if($count==1)
		return $array['gmlevel'];
	}
	
function CheckUserVote($tid, $uid)
	{
		$select = mysql_query("SELECT * FROM `vote_var` WHERE `topid`='$tid' AND `userid`='$uid'");
		$array = mysql_fetch_array($select);
		$count = mysql_num_rows($select);
		if ($count == 0)
			{ return "<input type='button' onclick=location.href='./module/dovote.php?tid=$tid' value='Проголосовать'>"; }
		else
			{
				$last_votetime = $array["votetime"];
				$time = GetDataTime(gmtime() - 86400);
				if($last_votetime < $time)
					{ return "<input type='button' onclick=location.href='./module/dovote.php?tid=$tid' value='Проголосовать'>"; }
				else 
					{ return "<i><font color='red'>Уже голосовали!</font></i>"; }
			}
	}	

/*function checkban($uid)
	{
		$select = mysql_query("SELECT * FROM `account_banned` WHERE `id`=$uid");
		if(mysql_num_rows($select)!=0) {
			$array = mysql_fetch_array($select);
			$active = $array['active'];
			if($active==0) return false;
			else return true;
		}
		else return false;
	}*/
	
function getiteminfo($itm)
	{
		SelectDB(imwcp);
		$q = "SELECT * FROM `item_list` WHERE `item_id`='%d'";
		$query = sprintf($q, $itm);
		$select = mysql_query($query);
		return $select;
	}
	
function chechitem($itm)
	{
		SelectDB(imwcp);
		$q = "SELECT * FROM `item_list` WHERE `item_id`='%d'";
		$query = sprintf($q, $itm);
		$select = mysql_query($query);
		$count = mysql_num_rows($select);
		if($count==1) return true; else return false;
	}

function buyitem($itmid, $chr)
	{
		if(chechitem($itmid)) {
		$uid = $_SESSION["uid"];
		if(charinacc($chr, $uid)) {
			$bonuses = getbonuses();
			$itemarray = mysql_fetch_array(getiteminfo($itmid));
			$itemprice = $itemarray['price'];
			if($bonuses>$itemprice) {
				SelectDB(char);
				// mail
				$s_maxmailid = mysql_query("SELECT MAX(id) FROM `mail` LIMIT 1");
				$mailid_array = mysql_fetch_array($s_maxmailid);
				$temp_mid = $mailid_array['MAX(id)'];
				$mid = ($temp_mid+10);
				// item
				$s_maxitemid = mysql_query("SELECT MAX(guid) FROM `item_instance` LIMIT 1");
				$itemid_array = mysql_fetch_array($s_maxitemid);
				$temp_itemid = $itemid_array['MAX(guid)'];
				$itemguid = ($temp_itemid+10);
				// 
				mysql_query("INSERT INTO `item_instance` (`guid`,`itemEntry`) VALUES ('$itemguid','$itmid')");
				mysql_query("INSERT INTO `mail_items` VALUES ('$mid','$itemguid','$chr')");
				mysql_query("INSERT INTO `mail` (`id`,`messageType`,`stationery`,`receiver`,`subject`,`body`,`has_items`) VALUES ('$mid',0,61,'$chr','Donate!','Спасибо за помощь серверу! Примите этот подарок от нашей команды!','1')");
				SelectDB(auth);
				$bon = ($bonuses-$itemprice);
				mysql_query("UPDATE `account` SET `bonuses`='$bon' WHERE `id`='$uid'");
				echo "<center>Предмет отправлен выбраному персонажу на почту! У вас снято <b>".$itemprice."</b> бонусов!</center>";
			} else echo "<center>У вас недостаточно бонусов!</center>";		
		} else echo "<center><b>Ошибка!</b></center>";
		} else echo "<center><b>Этот предмет невозможно купить!</b></center>";
	}

?>